<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Sample Events Class
 * 
 * @package     PyroCMS
 * @subpackage  Sample Module
 * @category    events
 * @author      PyroCMS Dev Team
 */
class Events_Channel 
{
    protected $ci;
    
    public function __construct()
    {
        $this->ci =& get_instance();
        // register the public_controller event when this file is autoloaded
        Events::register('post_user_register', array($this, 'post_user_register'));
        
        Events::register('post_user_login', array($this, 'post_user_login'));
        
        Events::register('lr_acc_change', array($this, 'update_user_lr_acc'));
        
        $this->ci->load->model('channel/l_user_m');
    	$this->ci->load->model('channel/transaction_m');
    }
    
    public function post_user_register($user_id) {
    	$this->add_l_user($user_id);
    }
    
    public function post_user_login ($user_id) {
    	$user_id = $this->ci->session->userdata('id');
    	$l_user = $this->ci->l_user_m->get_l_user_data($user_id);
    	$this->ci->session->set_userdata($l_user);
    	
    }
    
    public function update_user_lr_acc($new_lr_acc) {
    	$current_lr_acc = $this->ci->session->userdata('lr_acc');
    	if ($current_lr_acc != $new_lr_acc) {
    		$this->ci->session->set_userdata('lr_acc', $new_lr_acc);
    		$user_id = $this->ci->session->userdata('id');
    		if ($user_id) {
    			$this->ci->l_user_m->update_user_lr_acc($user_id, $new_lr_acc);
    		}
    	}
    }
    
    // this will be triggered by the Events::trigger('public_controller') code in Public_Controller.php
    private function add_l_user($user_id)
    {
    	$lr_acc = '';
    	if (isset($_REQUEST['lr_acc'])) {
    		$lr_acc = $_REQUEST['lr_acc'];
    	}
    	$this->ci->l_user_m->add_l_user($user_id, 'USER', 0, 0, 0, 0, 0, 0, $lr_acc);
    	
    	$now = new DateTime();
    	
    	$this->ci->transaction_m->add_one_transaction(array(
		    	'user_id' => $user_id,
		        'trans_type' => 'DEPOSIT',
		        'trans_date' => $now->format('Y-m-d H:i:s'),
		        'settle_date' => $now->format('Y-m-d H:i:s')));
    }
    
}
/* End of file events.php */